
import { _decorator, Component, Node, Prefab, Label, LabelComponent } from 'cc';
import { NetWork } from './NetWork';
const { ccclass, property } = _decorator;

@ccclass('LoginUI')
export class LoginUI extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;
    // @property(Label)     // 使用 property 装饰器声明属性，括号里是属性类型，装饰器里的类型声明主要用于编辑器展示
    // label1: Label = null;

    @property(Node)
    node1: Node = null; 

    @property(Node)
    login: Node = null; 

    // @property({ displayName: "NPC 预制件", type: Label })
    // nodelab: Label | undefined;
    
    private  network:NetWork|undefined=undefined;

    start () {
        // [3]
    }

    onClick(){
        
        this.network = new NetWork();
        this.network.Connect("ws://127.0.0.1:8101",this.OnConnectSuccess.bind(this),this.OnWebSocketEvent.bind(this));

        // this._label.enabled = true;
        
    }

    OnWebSocketEvent(eventName:any, arg:any){
        if (eventName == "OnReceive") {
            var str = arg[1]
            // this.nodelab?.string = str;
            this.node1.active = true;
            var text = this.node1.getComponent(LabelComponent);
            text.string = arg[1];

            this.login.active = false;
            console.log(arg[1])
            
        }
    }

    OnConnectSuccess(){
        console.log("连接成功1")

        var head = {
            messageType: 1,
            event: "gate.c1004login.login",
            sequence: 1
        };

        this.network?.write(head,"123456");

    }

    // update (deltaTime: number) {
    //     // [4]
    // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
